package user

import (
	"github.com/gin-gonic/gin"
	"server/app/api/request/common"
	group2 "server/common/dao/group"
	"server/common/utils"
	"server/pkg/response"
)

var Group = group{}

type group struct{}

func (group) Info(c *gin.Context) {
	// 获取用户id
	roleId := utils.GetUserId(c)

	// 查询用户绑定的群组
	groupInfo, e := group2.GroupDao.GetBindGroupByUserId(roleId)
	if e != nil {
		response.Error(c, e.Error())
		return
	}

	// 查询群组成员
	groupMembers := group2.GroupDao.GetGroupMemberList(groupInfo.Id)

	// 返回数据
	response.Success(c, gin.H{"group_info": groupInfo, "group_members": groupMembers})
}

func (group) Message(c *gin.Context) {
	var req common.PageReq
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error())
		return
	}

	// 获取用户id
	roleId := utils.GetUserId(c)

	// 查询用户绑定的群组
	groupInfo, e := group2.GroupDao.GetBindGroupByUserId(roleId)
	if e != nil {
		response.Error(c, e.Error())
		return
	}

	// 查询群组消息
	groupMessages := group2.GroupDao.GetGroupMessageList(groupInfo.Id, req)

	// 返回数据
	response.Success(c, gin.H{"group_messages": groupMessages})
}
